//
//  WPSigleTransPriceAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/9.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPSigleTransPriceAlertView.h"
#import "WPSigleTransPriceCell.h"
#import "WPRightManageDetailModel.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@interface WPSigleTransPriceAlertView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * tickTableView;/**< 票源列表 */
@property (nonatomic, strong) UIButton * sureBtn;/**< 确定按钮 */
@property (nonatomic, strong) NSArray * dataSource;/**< 数据源 */
@property (nonatomic, copy) void (^complete)(NSArray *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */


@end
@implementation WPSigleTransPriceAlertView
+ (WPSigleTransPriceAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithDatasource:(NSArray *)dataSource andWithComplete:(void (^)(NSArray *))complete{
    WPSigleTransPriceAlertView * view = [[WPSigleTransPriceAlertView alloc]initWithTitle:title withSize:rect withDataSource:dataSource WithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect withDataSource:(NSArray *)arr WithComplete:(void (^)(NSArray *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _complete = complete;
        _dataSource = arr;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.tickTableView];
    [self addSubview:self.sureBtn];
}
#pragma mark - responds events
- (void)respondsToSurnBtn {
    [self hide];
    NSMutableArray * arr = [NSMutableArray array];
    for (int i = 0; i < _dataSource.count; i++) {
        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{@"productId":(WPRightManageDetailModel *)[_dataSource[i] ticketId]}];
        WPSigleTransPriceCell * cell = [_tickTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (cell.sellPriceTextField.text.length == 0) {
            [dict removeAllObjects];
            break;
        }
        dict[@"salePrice"] = cell.sellPriceTextField.text;
        [arr addObject:dict];
    }
    _complete(arr);
}
#pragma mark - 协议UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
    WPSigleTransPriceCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[WPSigleTransPriceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    [cell setModel:_dataSource[indexPath.row]];
    return cell;
}
#pragma mark - UITableViewDelegate
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80*DHFlexibleHorizontalMutiplier();
}
#pragma mark - getter
- (UITableView *)tickTableView {
    if (!_tickTableView) {
        _tickTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 50*DHFlexibleHorizontalMutiplier(), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)-125*DHFlexibleHorizontalMutiplier()-2) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.bounces = NO;
            tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
            tableview;
        });
    }
    return _tickTableView;
}
- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), 220*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"确 定" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToSurnBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _sureBtn;
}
@end
